package com.runshu.newsmartark.dbutil;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;


import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;

import java.util.List;

/**
 * Demo class
 *
 * @author yu
 * @date 12/11/20
 * @decs：
 */
@Dao
public interface ProAllDao {
    /**
     * 查询商品全部数据
     *
     * @return
     */
    @Query("SELECT * FROM ProAllEntity")
    List<ProAllEntity> getAllProList();

    /**
     * 查询柜子中全部商品数据
     *
     * @return
     */
    @Query("SELECT * FROM proselfentity")
    List<ProAllEntity> getSelfProList();

    /**
     * 插入全部记录
     *
     * @param proAllList
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertOperRecords(List<OperRecordfEntity> proAllList);

    /**
     * 插入全部商品数据
     *
     * @param proAllList
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAllPro(List<ProAllEntity> proAllList);

    /**
     * 插入全部商品数据(有的话 不插入数据)
     * @param proAllList
     */
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insertAllProIgnore(List<ProAllEntity> proAllList);

    @Query("DELETE FROM ProSelfEntity")
    void deleteSelfPro();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertSelfPro(List<ProSelfEntity> selfList);

    @Update
    void updateProAll(List<ProAllEntity> lists);

    /**
     * 查询全部商品最后一条数据的更新时间
     *
     * @return
     */
    @Query("SELECT updateTime FROM ProAllEntity ORDER BY updateTime desc LIMIT 1")
    String getLastUpdateTime();

    /**
     * 查询商品本地数据 根据条件查询 (分组之后) 显示
     * @param category
     * @param modle
     * @param manufacturer
     * @return
     */
    @Query("SELECT productCode AS productNo,productName,regularModel AS productModel,COUNT(1)stock,IFNULL(vendorName,'暂无') AS manufacturer,productVendor " +
            "FROM ProSelfEntity " +
            "WHERE (:category IS NULL OR '' = :category OR categoryLevel2 = :category)" +
            " AND (:modle IS NULL OR '' = :modle OR productModel= :modle)" +
            " AND (:manufacturer IS NULL OR '' = :manufacturer OR productVendor = :manufacturer)" +
            "GROUP BY productCode,regularModel,productName,vendorName,productVendor ORDER BY productCode,stock DESC")
    List<ProductsBean> getProByType(String category, String modle, String manufacturer);

    /**
     * 查询商品本地数据 根据条件查询
     * @param productno
     * @param modle
     * @param manufacturer
     * @return
     */
    @Query("SELECT * " +
            "FROM ProSelfEntity " +
            "WHERE (:productno IS NULL OR '' = :productno OR productCode = :productno)" +
            " AND (:modle IS NULL OR '' = :modle OR regularModel = :modle)" +
            " AND (:manufacturer IS NULL OR '' = :manufacturer OR productVendor = :manufacturer)" +
            " ORDER BY productCode")
     List<ProSelfEntity> getProInfoByType(String productno, String modle, String manufacturer);

    /**
     * 获取商品在所属医院的流水码 商品数据集合 ProAllEntity类型
     * @param serialNos
     * @return
     */
    @Query("SELECT * FROM ProAllEntity  WHERE serialNo IN (:serialNos)")
    List<ProAllEntity> getProBySerialNos(String... serialNos);

    /**
     * 查找 当前的柜中商品 在所有商品中的数据集合  ProSelfEntity类型
     * @param serialNos
     * @return
     */
    @Query("SELECT * FROM ProAllEntity  WHERE serialNo IN (:serialNos)")
    List<ProSelfEntity> getProBySerialNosToSelfEntity(String... serialNos);

    /**
     * 获取下架商品数据集合 （分组之后的）
     * @param serialNos
     * @return
     */
    @Query("SELECT productCode AS productNo,categoryLevel2Name AS productCategory,0 AS isAdd ,productName,regularModel AS productModel,batchNo,validPeriodDate AS periodDate,IFNULL(vendorName,'暂无') AS manufacturer,COUNT(1)stockChange FROM ProSelfEntity " +
            "WHERE serialNo NOT IN  (:serialNos)GROUP BY productCode,regularModel,productName,vendorName,productVendor ORDER BY productCode,stockChange DESC")
    List<ConfirmStockEntity.AllProductsBean> getMenusPros(String... serialNos);

    /**
     * 获取下架的商品集合
     * @param serialNos
     * @return
     */
    @Query("SELECT * FROM ProSelfEntity " +
            "WHERE serialNo NOT IN  (:serialNos) ")
    List<ProAllEntity> getMenusProAllList(String... serialNos);

    /**
     * 获取上架的商品集合
     * @param serialNos
     * @return
     */
    @Query("SELECT * FROM proallentity " +
            "WHERE serialNo IN (:serialNos) " +
            " AND serialNo NOT IN(SELECT serialNo FROM ProSelfEntity) ")
    List<ProAllEntity> getAddProAllList(String... serialNos);

    /**
     * 获取上架商品数据集合 （分组之后的）
     * @param serialNos
     * @return
     */
    @Query("SELECT productCode AS productNo,categoryLevel2Name AS productCategory,1 AS isAdd ,productName,regularModel AS productModel,batchNo,validPeriodDate AS periodDate,IFNULL(vendorName,'暂无') AS manufacturer,COUNT(1)stockChange FROM proallentity " +
            "WHERE serialNo IN (:serialNos) " +
            " AND serialNo NOT IN(SELECT serialNo FROM ProSelfEntity) " +
            "GROUP BY productCode,regularModel,productName,vendorName,productVendor ORDER BY productCode,stockChange DESC")
    List<ConfirmStockEntity.AllProductsBean> getAddPros(String... serialNos);

    /**
     * 获取操作日志的集合
     * @return
     */
    @Query("SELECT * FROM OPERRECORDFENTITY")
    List<OperRecordfEntity> getLogListInfo();

    /**
     * 查询本柜子的流水号码
     * @return
     */
    @Query("SELECT serialNo FROM PROSELFENTITY")
    List<String> getSerialNo();

    /**
     * 删除操作日志表
     */
    @Query("DELETE FROM OPERRECORDFENTITY ")
    void deleteOpearInfo();

    /**
     * 删除操作日志表
     */
    @Delete
    void deleteOpearInfo(List<OperRecordfEntity>list);

}
